{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function, absolute_import\n",
    "from gm.api import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "juejin_token = os.environ['juejin_token']\n",
    "# 掘金的官网可以免费申请 token\n",
    "\n",
    "set_token(juejin_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "GmError",
     "evalue": "{\"status\": 1001, \"message\": \"无法连接到终端服务\", \"function\": \"history\"}",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mGmError\u001b[0m                                   Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m history_data \u001b[39m=\u001b[39m history(symbol\u001b[39m=\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39mSZSE.128025\u001b[39;49m\u001b[39m'\u001b[39;49m, frequency\u001b[39m=\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39m1d\u001b[39;49m\u001b[39m'\u001b[39;49m, start_time\u001b[39m=\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39m2021-04-14\u001b[39;49m\u001b[39m'\u001b[39;49m,end_time\u001b[39m=\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39m2023-08-18\u001b[39;49m\u001b[39m'\u001b[39;49m, df\u001b[39m=\u001b[39;49m\u001b[39mTrue\u001b[39;49;00m)\n",
      "File \u001b[1;32mc:\\Users\\xda\\.conda\\envs\\py38\\lib\\site-packages\\gm\\api\\query.py:599\u001b[0m, in \u001b[0;36mhistory\u001b[1;34m(symbol, frequency, start_time, end_time, fields, skip_suspended, fill_missing, adjust, adjust_end_time, df)\u001b[0m\n\u001b[0;32m    587\u001b[0m req \u001b[39m=\u001b[39m GetHistoryBarsReq(\n\u001b[0;32m    588\u001b[0m     symbols\u001b[39m=\u001b[39msymbol,\n\u001b[0;32m    589\u001b[0m     frequency\u001b[39m=\u001b[39mfrequency,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    596\u001b[0m     adjust_end_time\u001b[39m=\u001b[39madjust_end_time,\n\u001b[0;32m    597\u001b[0m )\n\u001b[0;32m    598\u001b[0m status, result \u001b[39m=\u001b[39m py_gmi_history_bars_pb(req\u001b[39m.\u001b[39mSerializeToString())\n\u001b[1;32m--> 599\u001b[0m check_gm_status(status)\n\u001b[0;32m    600\u001b[0m rsp \u001b[39m=\u001b[39m Bars()\n\u001b[0;32m    601\u001b[0m rsp\u001b[39m.\u001b[39mParseFromString(result)\n",
      "File \u001b[1;32mc:\\Users\\xda\\.conda\\envs\\py38\\lib\\site-packages\\gm\\api\\_errors.py:31\u001b[0m, in \u001b[0;36mcheck_gm_status\u001b[1;34m(status)\u001b[0m\n\u001b[0;32m     29\u001b[0m     message \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39m; \u001b[39m\u001b[39m\"\u001b[39m \u001b[39m+\u001b[39m ext_message\n\u001b[0;32m     30\u001b[0m function \u001b[39m=\u001b[39m sys\u001b[39m.\u001b[39m_getframe()\u001b[39m.\u001b[39mf_back\u001b[39m.\u001b[39mf_code\u001b[39m.\u001b[39mco_name\n\u001b[1;32m---> 31\u001b[0m \u001b[39mraise\u001b[39;00m GmError(status, message, function)\n",
      "\u001b[1;31mGmError\u001b[0m: {\"status\": 1001, \"message\": \"无法连接到终端服务\", \"function\": \"history\"}"
     ]
    }
   ],
   "source": [
    "history_data = history(symbol='SZSE.128025', frequency='1d', start_time='2021-04-14',end_time='2023-08-18', df=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>frequency</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>bob</th>\n",
       "      <th>eob</th>\n",
       "      <th>position</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>1d</td>\n",
       "      <td>98.700</td>\n",
       "      <td>99.129</td>\n",
       "      <td>98.700</td>\n",
       "      <td>98.900</td>\n",
       "      <td>16710</td>\n",
       "      <td>1.6536e+06</td>\n",
       "      <td>98.700</td>\n",
       "      <td>2021-04-14 00:00:00+08:00</td>\n",
       "      <td>2021-04-14 00:00:00+08:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>1d</td>\n",
       "      <td>98.903</td>\n",
       "      <td>99.320</td>\n",
       "      <td>98.900</td>\n",
       "      <td>99.199</td>\n",
       "      <td>12470</td>\n",
       "      <td>1.2355e+06</td>\n",
       "      <td>98.900</td>\n",
       "      <td>2021-04-15 00:00:00+08:00</td>\n",
       "      <td>2021-04-15 00:00:00+08:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>1d</td>\n",
       "      <td>98.901</td>\n",
       "      <td>99.487</td>\n",
       "      <td>98.900</td>\n",
       "      <td>99.287</td>\n",
       "      <td>25222</td>\n",
       "      <td>2.5013e+06</td>\n",
       "      <td>99.199</td>\n",
       "      <td>2021-04-16 00:00:00+08:00</td>\n",
       "      <td>2021-04-16 00:00:00+08:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>1d</td>\n",
       "      <td>99.201</td>\n",
       "      <td>99.446</td>\n",
       "      <td>99.132</td>\n",
       "      <td>99.280</td>\n",
       "      <td>34200</td>\n",
       "      <td>3.3957e+06</td>\n",
       "      <td>99.287</td>\n",
       "      <td>2021-04-19 00:00:00+08:00</td>\n",
       "      <td>2021-04-19 00:00:00+08:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>1d</td>\n",
       "      <td>99.300</td>\n",
       "      <td>99.800</td>\n",
       "      <td>99.300</td>\n",
       "      <td>99.650</td>\n",
       "      <td>19050</td>\n",
       "      <td>1.8962e+06</td>\n",
       "      <td>99.280</td>\n",
       "      <td>2021-04-20 00:00:00+08:00</td>\n",
       "      <td>2021-04-20 00:00:00+08:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>1d</td>\n",
       "      <td>100.006</td>\n",
       "      <td>107.900</td>\n",
       "      <td>100.006</td>\n",
       "      <td>106.300</td>\n",
       "      <td>4464447</td>\n",
       "      <td>4.7222e+08</td>\n",
       "      <td>99.650</td>\n",
       "      <td>2021-04-21 00:00:00+08:00</td>\n",
       "      <td>2021-04-21 00:00:00+08:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>1d</td>\n",
       "      <td>106.000</td>\n",
       "      <td>112.500</td>\n",
       "      <td>103.505</td>\n",
       "      <td>107.890</td>\n",
       "      <td>4579477</td>\n",
       "      <td>5.0238e+08</td>\n",
       "      <td>106.300</td>\n",
       "      <td>2021-04-22 00:00:00+08:00</td>\n",
       "      <td>2021-04-22 00:00:00+08:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>1d</td>\n",
       "      <td>108.000</td>\n",
       "      <td>112.890</td>\n",
       "      <td>103.220</td>\n",
       "      <td>103.700</td>\n",
       "      <td>3612113</td>\n",
       "      <td>3.8805e+08</td>\n",
       "      <td>107.890</td>\n",
       "      <td>2021-04-23 00:00:00+08:00</td>\n",
       "      <td>2021-04-23 00:00:00+08:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>1d</td>\n",
       "      <td>104.500</td>\n",
       "      <td>108.800</td>\n",
       "      <td>102.999</td>\n",
       "      <td>103.206</td>\n",
       "      <td>2368267</td>\n",
       "      <td>2.4896e+08</td>\n",
       "      <td>103.700</td>\n",
       "      <td>2021-04-26 00:00:00+08:00</td>\n",
       "      <td>2021-04-26 00:00:00+08:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>1d</td>\n",
       "      <td>103.000</td>\n",
       "      <td>103.205</td>\n",
       "      <td>101.208</td>\n",
       "      <td>102.099</td>\n",
       "      <td>668050</td>\n",
       "      <td>6.8183e+07</td>\n",
       "      <td>103.206</td>\n",
       "      <td>2021-04-27 00:00:00+08:00</td>\n",
       "      <td>2021-04-27 00:00:00+08:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        symbol frequency     open     high      low    close   volume  \\\n",
       "0  SZSE.128025        1d   98.700   99.129   98.700   98.900    16710   \n",
       "1  SZSE.128025        1d   98.903   99.320   98.900   99.199    12470   \n",
       "2  SZSE.128025        1d   98.901   99.487   98.900   99.287    25222   \n",
       "3  SZSE.128025        1d   99.201   99.446   99.132   99.280    34200   \n",
       "4  SZSE.128025        1d   99.300   99.800   99.300   99.650    19050   \n",
       "5  SZSE.128025        1d  100.006  107.900  100.006  106.300  4464447   \n",
       "6  SZSE.128025        1d  106.000  112.500  103.505  107.890  4579477   \n",
       "7  SZSE.128025        1d  108.000  112.890  103.220  103.700  3612113   \n",
       "8  SZSE.128025        1d  104.500  108.800  102.999  103.206  2368267   \n",
       "9  SZSE.128025        1d  103.000  103.205  101.208  102.099   668050   \n",
       "\n",
       "       amount  pre_close                       bob                       eob  \\\n",
       "0  1.6536e+06     98.700 2021-04-14 00:00:00+08:00 2021-04-14 00:00:00+08:00   \n",
       "1  1.2355e+06     98.900 2021-04-15 00:00:00+08:00 2021-04-15 00:00:00+08:00   \n",
       "2  2.5013e+06     99.199 2021-04-16 00:00:00+08:00 2021-04-16 00:00:00+08:00   \n",
       "3  3.3957e+06     99.287 2021-04-19 00:00:00+08:00 2021-04-19 00:00:00+08:00   \n",
       "4  1.8962e+06     99.280 2021-04-20 00:00:00+08:00 2021-04-20 00:00:00+08:00   \n",
       "5  4.7222e+08     99.650 2021-04-21 00:00:00+08:00 2021-04-21 00:00:00+08:00   \n",
       "6  5.0238e+08    106.300 2021-04-22 00:00:00+08:00 2021-04-22 00:00:00+08:00   \n",
       "7  3.8805e+08    107.890 2021-04-23 00:00:00+08:00 2021-04-23 00:00:00+08:00   \n",
       "8  2.4896e+08    103.700 2021-04-26 00:00:00+08:00 2021-04-26 00:00:00+08:00   \n",
       "9  6.8183e+07    103.206 2021-04-27 00:00:00+08:00 2021-04-27 00:00:00+08:00   \n",
       "\n",
       "   position  \n",
       "0         0  \n",
       "1         0  \n",
       "2         0  \n",
       "3         0  \n",
       "4         0  \n",
       "5         0  \n",
       "6         0  \n",
       "7         0  \n",
       "8         0  \n",
       "9         0  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "history_data.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "history_data_df =get_history_instruments('SZSE.128025', fields=None, start_date='2020-01-01', end_date='2023-08-10', df=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>conversion_price</th>\n",
       "      <th>underlying_symbol</th>\n",
       "      <th>listed_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>870</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>186.000</td>\n",
       "      <td>8.93</td>\n",
       "      <td>SZSE.002728</td>\n",
       "      <td>2017-12-28 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>871</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>185.060</td>\n",
       "      <td>8.93</td>\n",
       "      <td>SZSE.002728</td>\n",
       "      <td>2017-12-28 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>872</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>182.480</td>\n",
       "      <td>8.93</td>\n",
       "      <td>SZSE.002728</td>\n",
       "      <td>2017-12-28 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>873</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>191.000</td>\n",
       "      <td>8.93</td>\n",
       "      <td>SZSE.002728</td>\n",
       "      <td>2017-12-28 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>874</th>\n",
       "      <td>SZSE.128025</td>\n",
       "      <td>192.499</td>\n",
       "      <td>8.93</td>\n",
       "      <td>SZSE.002728</td>\n",
       "      <td>2017-12-28 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          symbol  pre_close  conversion_price underlying_symbol  \\\n",
       "870  SZSE.128025    186.000              8.93       SZSE.002728   \n",
       "871  SZSE.128025    185.060              8.93       SZSE.002728   \n",
       "872  SZSE.128025    182.480              8.93       SZSE.002728   \n",
       "873  SZSE.128025    191.000              8.93       SZSE.002728   \n",
       "874  SZSE.128025    192.499              8.93       SZSE.002728   \n",
       "\n",
       "                  listed_date  \n",
       "870 2017-12-28 00:00:00+08:00  \n",
       "871 2017-12-28 00:00:00+08:00  \n",
       "872 2017-12-28 00:00:00+08:00  \n",
       "873 2017-12-28 00:00:00+08:00  \n",
       "874 2017-12-28 00:00:00+08:00  "
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "history_data_df[['symbol','pre_close','conversion_price','underlying_symbol','listed_date']].tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SHSE.600019</td>\n",
       "      <td>0.29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SZSE.300274</td>\n",
       "      <td>0.59</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SHSE.600025</td>\n",
       "      <td>0.06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SZSE.300999</td>\n",
       "      <td>0.13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SHSE.601868</td>\n",
       "      <td>0.16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295</th>\n",
       "      <td>SZSE.002129</td>\n",
       "      <td>0.48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>296</th>\n",
       "      <td>SZSE.002304</td>\n",
       "      <td>0.45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>297</th>\n",
       "      <td>SHSE.600015</td>\n",
       "      <td>0.19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>298</th>\n",
       "      <td>SZSE.300450</td>\n",
       "      <td>0.18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>299</th>\n",
       "      <td>SZSE.002410</td>\n",
       "      <td>0.22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>300 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          symbol  weight\n",
       "0    SHSE.600019    0.29\n",
       "1    SZSE.300274    0.59\n",
       "2    SHSE.600025    0.06\n",
       "3    SZSE.300999    0.13\n",
       "4    SHSE.601868    0.16\n",
       "..           ...     ...\n",
       "295  SZSE.002129    0.48\n",
       "296  SZSE.002304    0.45\n",
       "297  SHSE.600015    0.19\n",
       "298  SZSE.300450    0.18\n",
       "299  SZSE.002410    0.22\n",
       "\n",
       "[300 rows x 2 columns]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_constituents(index='SHSE.000300', fields='symbol, weight', df=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>symbol</th>\n",
       "      <th>days_to_expire</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2022-12-12</td>\n",
       "      <td>CFFEX.IM2212</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2022-12-13</td>\n",
       "      <td>CFFEX.IM2212</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2022-12-14</td>\n",
       "      <td>CFFEX.IM2212</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2022-12-15</td>\n",
       "      <td>CFFEX.IM2212</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2022-12-16</td>\n",
       "      <td>CFFEX.IM2212</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         date        symbol  days_to_expire\n",
       "0  2022-12-12  CFFEX.IM2212               4\n",
       "1  2022-12-13  CFFEX.IM2212               3\n",
       "2  2022-12-14  CFFEX.IM2212               2\n",
       "3  2022-12-15  CFFEX.IM2212               1\n",
       "4  2022-12-16  CFFEX.IM2212               0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_contract_expire_rest_days(symbols='CFFEX.IM2212', start_date='2022-12-12', end_date='2022-12-16', trade_flag = True, df=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "im_data = get_history_symbol(symbol='CFFEX.IM2212', start_date='2022-12-12', end_date='2022-12-16', df=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>trade_date</th>\n",
       "      <th>position</th>\n",
       "      <th>settle_price</th>\n",
       "      <th>pre_settle</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>upper_limit</th>\n",
       "      <th>lower_limit</th>\n",
       "      <th>margin_ratio</th>\n",
       "      <th>multiplier</th>\n",
       "      <th>is_adjusted</th>\n",
       "      <th>...</th>\n",
       "      <th>listed_date</th>\n",
       "      <th>delisted_date</th>\n",
       "      <th>underlying_symbol</th>\n",
       "      <th>board</th>\n",
       "      <th>trade_n</th>\n",
       "      <th>option_type</th>\n",
       "      <th>option_margin_ratio1</th>\n",
       "      <th>option_margin_ratio2</th>\n",
       "      <th>call_or_put</th>\n",
       "      <th>conversion_start_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2022-12-12 00:00:00+08:00</td>\n",
       "      <td>40334</td>\n",
       "      <td>6665.80</td>\n",
       "      <td>6677.4</td>\n",
       "      <td>6684.8</td>\n",
       "      <td>7345.0</td>\n",
       "      <td>6009.8</td>\n",
       "      <td>0.15</td>\n",
       "      <td>200</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>2022-07-22 00:00:00+08:00</td>\n",
       "      <td>2022-12-16 00:00:00+08:00</td>\n",
       "      <td>SHSE.000852</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2022-12-13 00:00:00+08:00</td>\n",
       "      <td>36462</td>\n",
       "      <td>6604.40</td>\n",
       "      <td>6665.8</td>\n",
       "      <td>6654.6</td>\n",
       "      <td>7332.2</td>\n",
       "      <td>5999.4</td>\n",
       "      <td>0.15</td>\n",
       "      <td>200</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>2022-07-22 00:00:00+08:00</td>\n",
       "      <td>2022-12-16 00:00:00+08:00</td>\n",
       "      <td>SHSE.000852</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2022-12-14 00:00:00+08:00</td>\n",
       "      <td>27501</td>\n",
       "      <td>6565.40</td>\n",
       "      <td>6604.4</td>\n",
       "      <td>6604.8</td>\n",
       "      <td>7264.8</td>\n",
       "      <td>5944.0</td>\n",
       "      <td>0.15</td>\n",
       "      <td>200</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>2022-07-22 00:00:00+08:00</td>\n",
       "      <td>2022-12-16 00:00:00+08:00</td>\n",
       "      <td>SHSE.000852</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2022-12-15 00:00:00+08:00</td>\n",
       "      <td>15696</td>\n",
       "      <td>6602.60</td>\n",
       "      <td>6565.4</td>\n",
       "      <td>6573.6</td>\n",
       "      <td>7221.8</td>\n",
       "      <td>5909.0</td>\n",
       "      <td>0.15</td>\n",
       "      <td>200</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>2022-07-22 00:00:00+08:00</td>\n",
       "      <td>2022-12-16 00:00:00+08:00</td>\n",
       "      <td>SHSE.000852</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2022-12-16 00:00:00+08:00</td>\n",
       "      <td>0</td>\n",
       "      <td>6515.43</td>\n",
       "      <td>6602.6</td>\n",
       "      <td>6600.6</td>\n",
       "      <td>7923.0</td>\n",
       "      <td>5282.2</td>\n",
       "      <td>0.15</td>\n",
       "      <td>200</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>2022-07-22 00:00:00+08:00</td>\n",
       "      <td>2022-12-16 00:00:00+08:00</td>\n",
       "      <td>SHSE.000852</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 39 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                 trade_date  position  settle_price  pre_settle  pre_close  \\\n",
       "0 2022-12-12 00:00:00+08:00     40334       6665.80      6677.4     6684.8   \n",
       "1 2022-12-13 00:00:00+08:00     36462       6604.40      6665.8     6654.6   \n",
       "2 2022-12-14 00:00:00+08:00     27501       6565.40      6604.4     6604.8   \n",
       "3 2022-12-15 00:00:00+08:00     15696       6602.60      6565.4     6573.6   \n",
       "4 2022-12-16 00:00:00+08:00         0       6515.43      6602.6     6600.6   \n",
       "\n",
       "   upper_limit  lower_limit  margin_ratio  multiplier  is_adjusted  ...  \\\n",
       "0       7345.0       6009.8          0.15         200        False  ...   \n",
       "1       7332.2       5999.4          0.15         200        False  ...   \n",
       "2       7264.8       5944.0          0.15         200        False  ...   \n",
       "3       7221.8       5909.0          0.15         200        False  ...   \n",
       "4       7923.0       5282.2          0.15         200        False  ...   \n",
       "\n",
       "                listed_date             delisted_date  underlying_symbol  \\\n",
       "0 2022-07-22 00:00:00+08:00 2022-12-16 00:00:00+08:00        SHSE.000852   \n",
       "1 2022-07-22 00:00:00+08:00 2022-12-16 00:00:00+08:00        SHSE.000852   \n",
       "2 2022-07-22 00:00:00+08:00 2022-12-16 00:00:00+08:00        SHSE.000852   \n",
       "3 2022-07-22 00:00:00+08:00 2022-12-16 00:00:00+08:00        SHSE.000852   \n",
       "4 2022-07-22 00:00:00+08:00 2022-12-16 00:00:00+08:00        SHSE.000852   \n",
       "\n",
       "   board  trade_n  option_type  option_margin_ratio1  option_margin_ratio2  \\\n",
       "0      0        0                                0.0                   0.0   \n",
       "1      0        0                                0.0                   0.0   \n",
       "2      0        0                                0.0                   0.0   \n",
       "3      0        0                                0.0                   0.0   \n",
       "4      0        0                                0.0                   0.0   \n",
       "\n",
       "  call_or_put  conversion_start_date  \n",
       "0                               None  \n",
       "1                               None  \n",
       "2                               None  \n",
       "3                               None  \n",
       "4                               None  \n",
       "\n",
       "[5 rows x 39 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "im_data.tail(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.17"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
